package com.lagou.edu.course.remote;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lagou.edu.common.constant.MQConstant;
import com.lagou.edu.common.result.ResponseDTO;
import com.lagou.edu.common.util.ConvertUtils;
import com.lagou.edu.common.util.ValidateUtils;
import com.lagou.edu.course.api.CourseBossRemoteService;
import com.lagou.edu.course.api.dto.ActivityCourseDTO;
import com.lagou.edu.course.api.dto.CourseDTO;
import com.lagou.edu.course.api.dto.PageResultDTO;
import com.lagou.edu.course.api.dto.TeacherDTO;
import com.lagou.edu.course.api.param.CourseQueryParam;
import com.lagou.edu.course.entity.po.ActivityCourse;
import com.lagou.edu.course.entity.po.Course;
import com.lagou.edu.course.entity.po.Teacher;
import com.lagou.edu.course.service.IActivityCourseService;
import com.lagou.edu.course.service.ICourseService;
import com.lagou.edu.course.service.ITeacherService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/course")
public class CourseBossRemoteServiceImpl implements CourseBossRemoteService {

    @Autowired
    private ICourseService courseService;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    @RequestMapping(value = "/saveOrUpdateCourse",method = RequestMethod.POST)
    public ResponseDTO saveOrUpdateCourse(CourseDTO courseDTO) {
        ResponseDTO responseDTO = null;
        try{
            courseService.saveOrUpdate(ConvertUtils.convert(courseDTO, Course.class));
            responseDTO = ResponseDTO.success();
        } catch (Exception e){
            responseDTO = ResponseDTO.ofError(e.getMessage());
            throw e;
        }
        return responseDTO;
    }

    @Override
    @RequestMapping(value = "/getAllCourses",method = RequestMethod.GET)
    public List<CourseDTO> getAllCourses() {
        return ConvertUtils.convertList(courseService.list(),CourseDTO.class);
    }

    @Override
    @RequestMapping(value = "/getCourseById",method = RequestMethod.GET)
    public CourseDTO getCourseById(Integer courseId) {
        ValidateUtils.notNullParam(courseId,"课程ID不能为空");
        return ConvertUtils.convert(courseService.getById(courseId),CourseDTO.class);
    }

    @Override
    @RequestMapping(value = "/getQueryCourses",method = RequestMethod.POST)
    public PageResultDTO<CourseDTO> getQueryCourses(CourseQueryParam courseQueryParam) {
        ValidateUtils.notNullParam(courseQueryParam.getCurrentPage(),"当前页号不能为空");
        ValidateUtils.notNullParam(courseQueryParam.getPageSize(),"页数不能为空");
        String courseName = courseQueryParam.getCourseName();
        Integer status = courseQueryParam.getStatus();

        Page<Course> page = new Page<>(courseQueryParam.getCurrentPage(),
                courseQueryParam.getPageSize());
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(courseName)){
            queryWrapper.like("course_name",courseName);
        }
        if(null!=status){
            queryWrapper.eq("status", status);
        }
        int count = courseService.count(queryWrapper);
        queryWrapper.orderByDesc("id");
        IPage<Course> coursePage = courseService.selectPage(page, queryWrapper);
        PageResultDTO<CourseDTO> result = new PageResultDTO<>();
        BeanUtil.copyProperties(coursePage, result);
        result.setTotal(count);
        return result;
    }

    @Override
    @RequestMapping(value = "/changeState",method = RequestMethod.GET)
    public ResponseDTO changeState(Integer courseId, Integer status) {
        ValidateUtils.notNullParam(courseId,"课程ID不能为空");
        ValidateUtils.notNullParam(status,"状态不能为空");
        ResponseDTO responseDTO = null;
        Course course = new Course();
        course.setId(courseId);
        course.setStatus(status);
        try {
            courseService.updateById(course);
            if(status == 1){
                //课程上架
                sendCourseUpMsg(courseId);
            }
            responseDTO = ResponseDTO.success();
        }catch (Exception e){
            responseDTO = ResponseDTO.ofError(e.getMessage());
            throw  e;
        }
        return responseDTO;
    }

    private void sendCourseUpMsg(Integer courseId) {

        Course course = courseService.getById(courseId);

        rocketMQTemplate.asyncSend(MQConstant.Topic.LESSON_RELEASE_SEND_MESSAGE, course.getCourseName(), new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                log.info("返回状态是"+sendResult.getSendStatus().toString());
            }

            @Override
            public void onException(Throwable e) {
                e.printStackTrace();
            }
        });
    }
}
